package com.wjc.httpserver;


import java.net.UnknownHostException;
import com.wjc.httpsettings.HttpServerSettings;
import com.wjc.httpsettings.ProxySettings2;

import socks.Socks5Proxy;
import socks.UserPasswordAuthentication;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class HttpServerService extends Service {
	private static final String TAG="LocalHttpService";
	/**
	 * 代理服务器配置
	 * */
	
	private  static Socks5Proxy proxySet;
	HttpServer server;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	public void onCreate(){
		super.onCreate();
	//初始化代理协议和连接池
		String proxy ;
        int port ;
        String username;
        String password;
        int localPort;
		if(HttpServerSettings.getProxyHost()==null){
			Log.d(TAG, "读取默认配置");
		SharedPreferences  info = getSharedPreferences("Socket5Setting",0); 
        
         proxy = info.getString("proxyHost","192.168.2.104"); 
         port = info.getInt("proxyPort",1080);
         username=info.getString("username", "first");
         password=info.getString("password", "first");
         localPort=info.getInt("localPort", 8080);
		}else{
			proxy=HttpServerSettings.getProxyHost();
			port=HttpServerSettings.getProxyPort();
			username=HttpServerSettings.getUsername();
			password=HttpServerSettings.getPassword();
			localPort=HttpServerSettings.getLocalPort();
		}
		set(proxy,port,username,password,this,localPort);
	}
	public void onStart(Intent i,int id){
		Toast t=Toast.makeText(this, "正在启动代理", Toast.LENGTH_LONG);
		t.show();
		server=HttpServer.newInstance();
		server.setPort(HttpServerSettings.getLocalPort());
		server.setProxySet(proxySet);
		server.start();
		
	}
	public void onDestroy(){
		server.stop();
		server=null;
		Toast t=Toast.makeText(this, "正在停止代理", Toast.LENGTH_LONG);
		t.show();
	}
	
	/**
	 * 设置httpserver的socket5代理服务器地址
	 * @author wjc
	 * @param host  代理服务器地址  //可以是域名
	 * @param port   端口号 
	 * @param username 用户名
	 * @param password 密码
	 * @throws UnknownHostException 如果代理服务器地址不合法将抛出该异常
	 * @return void
	 * */
	public static void setSocket5Proxy(String proxy,int port,String username,String password,Context context,int localPort) throws UnknownHostException{
			set(proxy,port,username,password,context,localPort);
	}
	private static void store(String proxy,int port,String username,String password,Context context,int localPort){
		Editor info = context.getSharedPreferences("Socket5Setting", 0).edit();  
		   info.putString("proxyHost",proxy);  
		   info.putInt("proxyPort", port);
		   info.putString("username",username);
		   info.putString("password",password);
		   info.putInt("localPort", localPort);
	}
	private static void set(String proxy,int port,String username,String password,Context context,int localPort){
		//端口检查
		if(port<0)port=1080;//小于0设置为默认
		try {
			proxySet = new Socks5Proxy(null, proxy, port);
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		//认证设置,如果username为空则不添加
		if(username!=null){
			UserPasswordAuthentication k=new UserPasswordAuthentication(username,password);
			proxySet.setAuthenticationMethod(UserPasswordAuthentication.METHOD_ID, k);
		}
		store(proxy,port,username,password,context,localPort);
		((Socks5Proxy)proxySet).resolveAddrLocally(false);
		try{
		ProxySettings2.setProxy(context,"0.0.0.0" ,localPort);
		ProxySettings2.setDebug(true);
		}catch(Exception e){
			e.printStackTrace();
			Toast t=Toast.makeText(context, "设置webKit 失败 检查当前是否无网络 或处在wap下", Toast.LENGTH_LONG);
			t.show();
		}
		
		HttpServerSettings.set(proxy, port, username, password, localPort);
	}
	  
}
